﻿using Autofac.Core;
using Factory.Common;
using Factory.MasterData.WorkshopSetups;
using Factory.MasterData.WorkshopSetups.DTO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using NuGet.Protocol.Core.Types;
using Polly.Caching;
using System;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using VOL.WebApi;
using Volo.Abp.Domain.Repositories;

namespace Factory.Web.Controllers.MasterData.WorkshopsSetups
{
    public class WorkshopsSetupsController : Controller
    {
     
        private readonly IWorkshopSetupAppService workshopSetupAppService;
       

        public WorkshopsSetupsController(IWorkshopSetupAppService workshopSetupAppService)
        {
            this.workshopSetupAppService = workshopSetupAppService;
       
        }


        #region 控制器
        /// <summary>
        /// 查询工厂车间信息
        /// </summary>
        /// <param name="WorkShopCode"></param>
        /// <param name="WorkShopName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> ShowCar(int page, int limit, string? workShopCode, string? workShopName)
        {
            var pageData=await workshopSetupAppService.ShowWorkshopSetup(page, limit, workShopCode, workShopName);
            return Json(new { data = pageData.Queryable, code = 0,count=pageData.CurrentPage  });
        }
       

        /// <summary>
        /// 删除工厂车间设置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> DeleteWorkshopSetup(int id)
        {
            return await workshopSetupAppService.DeleteWorkshopSetup(id);
        }

        /// <summary>
        /// 添加工厂车间设置
        /// </summary>
        /// <param name="workshopSetup"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> CreateWorkshopSetup(AddWorkshopSetupDTO workshopSetupDTO)
        {
            // 编码为Base64字符串 加密
            string base64Encoded = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(workshopSetupDTO.WorkShopPic));


            var code = QRCodeHelper.CreateQRCode(base64Encoded);
            workshopSetupDTO.WorkShopPic = code;
            int result = await workshopSetupAppService.CreateWorkshopSetup(workshopSetupDTO);
            return result;
        }

        /// <summary>
        /// 根据id查询工厂车间信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<WorkshopSetupsDTO> GetByIdWorkshopSetup(int id)
        {
            var workshop =await workshopSetupAppService.GetByIdWorkshopSetup(id);
            return workshop;
        }


        /// <summary>
        /// 修改工厂车间信息
        /// </summary>
        /// <param name="workshop"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateWorkshopSetupStatus(WorkshopSetupsDTO workshopDTO)
        {
                        
               //删除原有图片
               QiNiu.DeleteTheLocal(workshopDTO.WorkShopPic);
            // 编码为Base64字符串 加密
            string base64Encoded = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(workshopDTO.WorkShopPic));


            var code = QRCodeHelper.CreateQRCode(base64Encoded);
            workshopDTO.WorkShopPic = code;

            int result = await workshopSetupAppService.UpdateWorkshopSetupStatus(workshopDTO);
            return result;
        }


        #endregion


        #region 视图页面
        /// <summary>
        /// 显示工厂车间设置页面
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowWorkshopsSetups()
        {
            return View();
        }

        /// <summary>
        /// 添加工厂车间设置页面
        /// </summary>
        /// <returns></returns>
        public IActionResult AddWorkshopsSetups()
        {
            return View();
        }

        /// <summary>
        /// 编辑工厂车间设置页面
        public IActionResult EditWorkshopSetup()
        {
            ViewBag.id = Request.Query["id"].ToString();
            return View();
        }

        //车间详情页面    
        public IActionResult DetailWorkshop()
        {
            ViewBag.id = Request.Query["id"].ToString();
            return View();
        }



        #endregion
    }
}
